package com.zygh.lz.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zygh.lz.entity.*;
import com.zygh.lz.entity.remould.Xareas;
import com.zygh.lz.entity.vo.JobInfoVo;
import com.zygh.lz.vo.StatisticsVo;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.security.core.parameters.P;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public interface XareaMapper extends BaseMapper<Xarea> {
    int deleteByPrimaryKey(Integer id);

    int insert(Xarea record);

    int insertSelective(Xarea record);

    Xarea selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Xarea record);

    int updateByPrimaryKeyWithBLOBs(Xarea record);

    int updateByPrimaryKey(Xarea record);

    //根据大队，中队，岗位，中队领导
    List<Xarea> selectXareabycondition(@Param("battalion") String battalion,
                                       @Param("detachment") String detachment,
                                       @Param("station") String station,
                                       @Param("leader") String leader,
                                       @Param("grid") String grid,
                                       @Param("type") String type);

    //根据名字模糊查询
    List<Xarea> selectXareaByName(@Param("Name") String Name,
                                  @Param("battalion") String battalion, @Param("detachment") String detachment);

    //查询所有区域
    List<Xarea> selectXareaAll();

    List<Xarea> selectXareaZgByStaffId(@Param("id") Integer id, @Param("station") String station);

    List<Xarea> selectXareaByid(Integer id);

    List<HashMap> countYDSum(@Param("station") String station, @Param("battalion") String battalion, @Param("detachment") String detachment);

    List<HashMap> countYDSumby(@Param("station") String station,
                               @Param("battalion") String battalion,
                               @Param("detachment") String detachment);

    List<HashMap> countYxSum(@Param("conment") String conment,
                             @Param("battalion") String battalion,
                             @Param("detachment") String detachment);

    List<HashMap> countRcYDsumC2(String battalion);

    List<HashMap> countRcYDsum(String battalion);

    List<Integer> countWgSum(String battalion);

    List<Integer> countZdYDSum(String battalion);

    List<Integer> countQtYDSum(@Param("battalion") String battalion, @Param("detachment") String detachment);

    int countZSum(@Param("station") String station, @Param("battalion") String battalion,
                  @Param("detachment") String detachment, @Param("conment") String conment);

    int countZSumby(@Param("station") String station, @Param("battalion") String battalion,
                    @Param("detachment") String detachment, @Param("conment") String conment);

    int countQTZSum(@Param("battalion") String battalion, @Param("detachment") String detachment);

    List<Integer> countZDRc(@Param("battalion") String battalion, @Param("detachment") String detachment, @Param("station") String station);

    List<Integer> countZDRcC2(@Param("battalion") String battalion, @Param("detachment") String detachment);

    List<HashMap> countZDYxorTq(@Param("conment") String conment, @Param("battalion") String battalion, @Param("detachment") String detachment);

    List<String> findDd();

    List<String> findZd(@Param("battalion") String battaliont, @Param("conment") String conmen);

    List<Integer> countKsZ(@Param("station") String station, @Param("battalion") String battalion, @Param("detachment") String detachment);

    List<Integer> countTQZ(String battalion);

    List<Integer> countGsOrKsYDSum(@Param("station") String station, @Param("battalion") String battalion, @Param("conment") String conmen, @Param("changeShifts") String changeShifts);

    //查询夜间快速大队警组，警员
    List<Xarea> selectks();

    //夜巡组数
    List<Integer> countYxZ(@Param("battalion") String battalion,
                           @Param("detachment") String detachment);

    //夜巡应到
    List<Integer> countYxYDsum(String battalion);

    //查询其他
    List<Xarea> selectqt(@Param("conment") String conment, @Param("battalion") String battalion);


    /**
     * 日间警力部署
     */
    //日间固定岗部署人员按大队细分
    List<HashMap<String, Object>> selectfixationRJ(@Param("station") String station,
                                                   @Param("battalion") String battalion);

    //细分到中队
    List<HashMap<String, Object>> selectfixationzdRJ(@Param("station") String station,
                                                     @Param("battalion") String battalion);

    //日间重点单位岗
    List<HashMap> selectemphasisRJ(String battalion);

    //日间重点单位岗人员详情
    List<HashMap> selectemphasisPope(@Param("battalion") String battalion,
                                     @Param("detachment") String detachment);

    //细分到中队
    List<HashMap> selectemphasisZd();

    //日间铁骑
    List<HashMap<String, Object>> selectcavalryRJ(String battalion);

    //细分到中队
    List<HashMap<String, Object>> selectcavalryzdRJ();

    //日间网格警组
    List<HashMap<String, Object>> selectgriddingRJ(String battalion);

    //日间网格警力详情
    List<HashMap> selectgriddingPope(@Param("battalion") String battalion,
                                     @Param("detachment") String detachment);

    //日间网格警力详情细分到中队
    List<HashMap<String, Object>> selectgriddingZD();

    //日间高速/快速
    List<HashMap> selectexpresswayRJ(@Param("station") String station, @Param("battalion") String battalion);

    //细分到中队
    List<HashMap> selectexpresswayZD();

    List<HashMap> selectexpresswayPope(@Param("station") String station,
                                       @Param("battalion") String battalion,
                                       @Param("conment") String conment,
                                       @Param("detachment") String detachment);

    //日间其他警力部署
    List<HashMap> selectqtRJ(@Param("stafftype") String stafftype, @Param("battalion") String battalion);

    //日间其他警力部署细分到中队
    List<HashMap> selectqtZD();

    //日间其他警员详情
    List<HashMap> selectqtPope(@Param("battalion") String battalion,
                               @Param("detachment") String detachment,
                               @Param("stafftype") String stafftype);

    //在线人警力部署
    List<HashMap> selectInformant();

    //在线人警力部署中队
    List<HashMap> selectInformantZD();

    //在线人详情
    List<HashMap> selectInformantPope(@Param("battalion") String battalion,
                                      @Param("detachment") String detachment);

    //夜间夜巡警力部署
    List<HashMap> selectNightTour(String battalion);

    //夜间夜巡警力部署细分到中队
    List<HashMap> selectNightTourZD();

    //夜间夜巡警力部署详情
    List<HashMap> selectNightTourPope(@Param("battalion") String battalion,
                                      @Param("detachment") String detachment);

    //夜间快速警力部署
    List<HashMap> selectcelerity(@Param("station") String station, @Param("battalion") String battalion);

    //夜间快速警力部署详情
    List<HashMap> selectcelerityPope(@Param("station") String station,
                                     @Param("battalion") String battalion,
                                     @Param("detachment") String detachment);

    //夜间快速警力部署细分到中队
    List<HashMap> selectcelerityPopeZD();

    //夜间高速警力部署
    List<HashMap> selectceleritygs(@Param("station") String station, @Param("battalion") String battalion);

    //夜间高速警力部署详情
    List<HashMap> selectceleritygsPope(@Param("station") String station, @Param("battalion") String battalion,
                                       @Param("detachment") String detachment);

    //夜间高速警力部署细分到中队
    List<HashMap> selectceleritygsPopeZD();

    //夜间其他
    List<HashMap> selectqita(String battalion);

    //夜间其他详情
    List<HashMap> selectqitaPope(@Param("battalion") String battalion,
                                 @Param("detachment") String detachment,
                                 @Param("stafftype") String stafftype);

    //九主六块十六示范区
    List<Xarea> selectDemonstrationPlot(@Param("station") String station, @Param("battalion") String battalion);

    //根据区域名字模糊匹配部署警力
    List<HashMap> selctStrength(@Param("name") String name, @Param("battalion") String battalion);

    //查询区域最后一条
    Xarea selectXareaEnd();

    //高峰岗部署警力
    List<Xarea> selectXareapolic(@Param("station") String station,
                                 @Param("battalion") String battalion, @Param("detachment") String detachment);

    /*日常勤务*/
    Integer countGFYD(@Param("battalion") String battalion, @Param("detachment") String detachment);

    Integer countGDYD(@Param("battalion") String battalion, @Param("detachment") String detachment);

    Integer countZDYD(@Param("battalion") String battalion, @Param("detachment") String detachment);

    Integer countTQYD(@Param("battalion") String battalion, @Param("detachment") String detachment);

    Integer countWGYD(@Param("battalion") String battalion, @Param("detachment") String detachment);

    Integer countGSYD(@Param("station") String station,
                      @Param("battalion") String battalion,
                      @Param("detachment") String detachment,
                      @Param("conment") String conment);

    Integer countKSYD(@Param("battalion") String battalion, @Param("detachment") String detachment, @Param("station") String station);

    List<String> countYXYD(@Param("battalion") String battalion, @Param("detachment") String detachment);

    List<HashMap> countJDYD(@Param("battalion") String battalion, @Param("detachment") String detachment);

    //统计辅警部署人
    List<HashMap> selectAssistPolice(@Param("station") String station,
                                     @Param("battalion") String battalion,
                                     @Param("detachment") String detachment,
                                     @Param("stafftype") String stafftype,
                                     @Param("gridding") String gridding,
                                     @Param("conment") String conment);

    /**
     * 对接市局接口
     * 根据不同条件查询标注信息
     */
    List<Xarea> selectXareaByInfo(Xarea xarea);

    List<HashMap> selectPoliceNumber(Xarea xarea);

    //根据区域名字模糊匹配部署警力
    List<HashMap> selctStrengthById(Xarea xarea);

    //任务组信息
    List<Xarea> selectTaskSetInfo();

    //任务信息
    List<Xarea> selectTaskInfo();

    //三级菜单任务信息
    List<Section> findLevelMenu(Integer sectionid);

    //网格下任务信息
    List<Section> findGridMenu(@Param("sectionid") Integer sectionid,
                               @Param("gridding") String gridding);

    //根据id,大队，岗位查询区域详情
    List<Regional> findDetailsByInfo(@Param("id") Integer id,
                                     @Param("sectionDid") Integer sectionDid,
                                     @Param("grid") String grid);

    //根据区域id查询人员详情
    List<Dailyservice> findStaffInfo(Integer id);

    //对接长峰科技接口
    List<Section> findLevelMenucf(Integer sectionid);

    //各大队 高峰岗、固定岗、快速岗、机动岗、重点机关岗、高峰岗、高速岗 列表查询
    List<Section> findGangById(@Param("sectionId") Integer sectionId, @Param("station") String station);

    //各大队 网格、夜巡、铁骑 列表查询
    List<Section> findMianXian(@Param("sectionId") Integer sectionId, @Param("station") String station);

    //各大队 机关列表查询
    List<Section> findJG(@Param("sectionId") Integer sectionId, @Param("detachment") String detachment);

    //空岗统计 当天没有签到的岗位统计
    List<HashMap> konggangRemind(String battalion);

    //空岗统计 当天没有签到的岗位统计详情
    List<HashMap> konggangRemindmend(@Param("battalion") String battalion, @Param("station") String station);

    List<HashMap> konggangnum(String battalion);

    List<String> findDdBystation(String station);

    List<String> findGangByName(@Param("battalion") String battalion, @Param("station") String station);

    Integer countByName(@Param("name") String name, @Param("battalion") String battalion);

    List<Xarea> findBySectionDid(Integer sectionDid);

    List<Xarea> findBySectionDidNoRW(Integer sectionDid);

    /**
     * 更新任务id
     *
     * @param xarea 封装的参数
     * @return int
     */
    int updateRwid(Xarea xarea);

    /**
     * 该部门签到的岗位
     *
     * @param sysSectionId 中队id
     * @return 签到岗位个数
     */
    int signPost(Integer sysSectionId);

    /**
     * 查缺勤人数
     *
     * @param startTime    开始时间
     * @param endTime      结束时间
     * @param dayDiff      天数
     * @param sysSectionId 中队id
     * @return 缺勤数组
     */
    List<String> getOmission(@Param("startTime") String startTime,
                             @Param("endTime") String endTime,
                             @Param("dayDiff") long dayDiff,
                             @Param("sysSectionId") Integer sysSectionId);

    /**
     * 根据岗位类型和中队id查询岗位
     *
     * @param station
     * @param id
     * @return
     */
    List<Xarea> findXareaByInfo(@Param("station") String station,
                                @Param("id") Integer id);

    /**
     * 根据部门和岗位查询 每天需要签到的岗位数量
     *
     * @param conment
     * @return
     */
    int findXareanumbers(@Param("conment") Integer conment,
                         @Param("sysSectionId") Integer sysSectionId);


    /**
     * 日常大队统计数量
     *
     * @return
     */
    List<Xarea> findrcSum(@Param("conment") Integer conment);

    /**
     * 日常大队统计数量
     *
     * @return
     */
    List<Xarea> findgsSum(@Param("conment") Integer conment);

    /**
     * 查询应签到人次
     *
     * @param sysSectionId
     * @param stafftype
     * @param changeShifts
     * @return
     */
    List<Xarea> findShouldSum(@Param("sysSectionId") Integer sysSectionId,
                              @Param("stafftype") String stafftype,
                              @Param("changeShifts") Integer changeShifts);


    /**
     * ================================PC端首页右侧岗位详情栏信息改造========================================
     */

    /**
     * 大队信息
     *
     * @param battalion 部门
     * @param station   岗位
     * @return 集合
     */
    List<Map> findBattalionInfo(@Param("battalion") String battalion,
                                @Param("station") String station);


    /**
     * 中队信息
     *
     * @param sectionDid 大队ID
     * @param station    岗位
     * @return 集合
     */
    List<Map> findDetachmentInfo(@Param("sectionDid") String sectionDid,
                                 @Param("station") String station);


    /**
     * 查询大队中队部署警力数量
     *
     * @param xareas 入参
     * @return int
     */
    int findPoliceStrength(Xareas xareas);

    //高峰期、夜巡部署警力查询
    int findPoliceStrengths(@Param("battalion") String battalion,
                            @Param("staffType") String staffType,
                            @Param("station") String station);

    //平峰期部署警力查询
    int findplateauStrengths(@Param("battalion") String battalion,
                             @Param("staffType") String staffType);


    /**
     * 查询大队中队岗位详情信息
     *
     * @param sectionZid 中队信息
     * @param station    岗位
     * @return 集合
     */
    List<Map> findStationInfo(@Param("sectionZid") Integer sectionZid,
                              @Param("station") String station);

    /**
     * 查询大队中队警力信息
     *
     * @param xareas 入参
     * @return int
     */
    List<Map> findPoliceStrengthInfo(Xareas xareas);

    //查询所有有岗位的大队
    List<Map> findXareaBattalion(Integer sectionId);

    //查询本大队岗位
    List<Map> findStation(String sectionId);

    //查询本大队岗位详情
    List<Map> findXareaByStation(@Param("sectionId") String sectionId,
                                 @Param("station") String station,
                                 @Param("name") String name);

    //日常勤务岗位类型
    List<Map<String, Object>> findStationByXarea(Integer sectionId);

    List<Map<String, Object>> findStationDetail(@Param("sectionId") Integer sectionId,
                                                @Param("station") String station);

    List<Map> findXareaCoordinate(@Param("battalion") String battalion,
                                  @Param("station") String station);


    List<Map> findStationBySection(String battalion);

    List<Map> findStationByNight(String battalion);

    List<Map> finSStationBysectionId(@Param("sectionId") Integer sectionId,
                                     @Param("station") String station);

    /**
     * =====================================领导驾驶舱，巡查总里程================================================
     */

    List<Map> findDeploySection(Integer id);

    //岗位统计
    Map findXareaByType(@Param("sectionId") Integer sectionId,
                        @Param("station") String station);


    //签到的岗位
    Integer findSignXarea(@Param("sectionId") Integer sectionId,
                          @Param("startTime") String startTime,
                          @Param("endTime") String endTime);


    Integer findLocationPolice(@Param("sectionId") Integer sectionpid);

    List<Map<String, Object>> selectExportCount();

    @Select("<script> SELECT COUNT(*) as signPostNum  FROM (SELECT * FROM xarea WHERE whether = 1" +
            " <if test='sectionDid != null'> AND section_Did = #{sectionDid} </if>" +
            " <if test='modeCode != null'> and pattern = #{modeCode} </if> ) x " +
            " WHERE EXISTS (SELECT * FROM sign WHERE signintime  like concat(#{today},'%') AND x.id=assignmentid)" +
            " </script> ")
    StatisticsVo order(@Param("sectionDid") Integer sectionDid, @Param("modeCode") Integer modeCode, @Param("today") String today);

    //@Select("<script>SELECT COUNT(*) as  shouldPostNum FROM (SELECT * FROM xarea WHERE whether = 1 " +
    //        " <if test = 'sectionDid != null'> " +
    //        "AND section_Did = #{sectionDid}" +
    //        "</if>" +
    //        " <if test='modeCode != null'>" +
    //        " and pattern = #{modeCode} " +
    //        " </if>) x" +
    //        " </script> ")
    //StatisticsVo orderAll(@Param("sectionDid")Integer sectionDid,@Param("modeCode")Integer modeCode);


    @Select("<script>  " +
            " select sum(police)+sum(auxiliary) from xarea " +
            " <where>" +
            " <if test='sectionDid != null'> " +
            "  section_Did = #{sectionDid}" +
            " </if>" +
            " <if test='postCode != null'>" +
            " and pattern in \n" +
            "<foreach  item=\"item\" collection=\"postCode\" index=\"index\"  open=\"(\" separator=\",\" close=\")\">\n" +
            " #{item}\n" +
            "</foreach> " +
            "</if>" +
            " </where>" +
            " </script>")
    Integer findPolice(@Param("sectionDid") Integer sectionDid, @Param("postCode") int[] postCode);


    @Select("<script> SELECT * FROM xarea x WHERE whether = 1" +
            " <if test='sectionDid != null'> AND section_Did = #{sectionDid} </if>" +
            " <if test='modeCode != null'> and pattern = #{modeCode} </if>  " +
            " and not EXISTS (SELECT * FROM sign WHERE signintime  like concat(#{today},'%') AND x.id=assignmentid)" +
            " </script> ")
    List<Xarea> empty(@Param("sectionDid") Integer sectionId, @Param("modeCode") Integer modeCode, @Param("today") String today);


    //@Select("<script> SELECT * FROM (SELECT * FROM xarea WHERE  " +
    //        " <if test='postCode != null'> " +
    //        "   post_code in " +
    //        "<foreach  item=\"item\" collection=\"postCode\" index=\"index\"  open=\"(\" separator=\",\" close=\")\"> " +
    //        " #{item} " +
    //        "</foreach> " +
    //        " </if> "+
    //        " AND detachment IS not NULL ) a " +
    //        "left JOIN (SELECT * FROM sign WHERE signintime like concat(#{today},'%') GROUP BY assignmentid) b ON  a.id = b.assignmentid  " +
    //        "LEFT JOIN section sc on b.sectionid=sc.sys_section_id <where> " +
    //        " <if test = 'sectionDid != null'> " +
    //        "  sc.section_pid = #{sectionDid} OR b.sectionid = #{sectionDid}" +
    //        "</if>" +
    //        " </where> and b.id is null" +
    //        " </script> ")

    //空岗数量=应签岗位与已签到岗位的 差集
    @Select("<script> SELECT * FROM (SELECT * FROM xarea WHERE whether = 1 " +
            " <if test='postCode != null'> " +
            "  and post_code in " +
            "<foreach  item=\"item\" collection=\"postCode\" index=\"index\"  open=\"(\" separator=\",\" close=\")\"> " +
            " #{item} " +
            "</foreach> " +
            " </if> " +
            "AND detachment IS NOT NULL) a  " +
            " LEFT JOIN (SELECT * FROM sign WHERE signintime  like concat(#{today},'%')   GROUP BY assignmentid) b ON  a.id = b.assignmentid  WHERE b.id is null " +
            " <if test = 'sectionDid != null'> " +
            "  and section_Did = #{sectionDid} " +
            "</if> ORDER BY section_Did,section_Zid " +
            " </script> ")
    List<Xarea> empty1(@Param("sectionDid") Integer sectionDid,
                       @Param("today") String today,
                       @Param("postCode") int[] postCode);

    @Select("<script> " +
            "SELECT  CASE section_Did\n" +
            "\tWHEN 300 THEN\n" +
            " ROUND(COUNT(*) / 3,0) \n" +
            "\tELSE\n" +
            "\t\tCOUNT(*)\n" +
            " END as `sum` FROM xarea <where> " +
            " <if test = 'sectionDid != null'> " +
            " section_Did = #{sectionDid}" +
            "</if>" +
            " <if test='postCode != null'>" +
            " and post_code in \n" +
            "<foreach  item=\"item\" collection=\"postCode\" index=\"index\"  open=\"(\" separator=\",\" close=\")\">\n" +
            " #{item} \n" +
            "</foreach> " +
            " </if>" +
            " AND detachment IS NOT NULL GROUP BY battalion ORDER BY  section_Did ASC" +
            " </where> " +
            " </script> ")
    List<Integer> orderAll1(@Param("sectionDid") Integer sectionDid, @Param("postCode") int[] postCode);

    @Select("<script>SELECT COUNT(*) as  shouldPostNum FROM (SELECT * FROM xarea WHERE whether = 1 AND detachment IS NOT NULL  " +
            " <if test = 'sectionDid != null'> " +
            "AND section_Did = #{sectionDid}" +
            "</if>" +
            " <if test='modeCode != null'>" +
            " and post_code in \n" +
            "<foreach  item=\"item\" collection=\"modeCode\" index=\"index\"  open=\"(\" separator=\",\" close=\")\">\n" +
            " #{item}\n" +
            "</foreach> " +
            " </if>) x" +
            " </script> ")
    StatisticsVo orderAll(@Param("sectionDid") Integer sectionDid, @Param("modeCode") int[] modeCode);


    List<Map<String, Object>> selectDailyService();

    @Select("<script> SELECT COUNT(*) as post,ifnull(SUM(police)+SUM(auxiliary),0) as peopel  FROM xarea WHERE detachment IS NOT NULL " +
            " <if test = 'sectionDid != null'> " +
            "AND section_Did = #{sectionDid}" +
            "</if>" +
            " <if test='postCode != null'>" +
            " and post_code in \n" +
            "<foreach  item=\"item\" collection=\"postCode\" index=\"index\"  open=\"(\" separator=\",\" close=\")\">\n" +
            " #{item}\n" +
            "</foreach> " +
            " </if> " +
            " </script> ")
    Map<String, Integer> getPingCount(@Param("sectionDid") String sectionDid, @Param("postCode") int[] postCode);

    List<Xarea> findByCondition(@Param("postCode") Integer postCode,
                                @Param("sectionIds") int[] sectionIds,
                                @Param("gridding") String gridding);

    List<JobInfoVo> getByPostId(@Param("id") Integer id);

    List<Section> selectSectionList();

    List<Map<String, Object>> selectExportPostCount();
}